/**
 * https://leetcode.cn/problems/flower-planting-with-no-adjacent/
 *
 * @param {number} n
 * @param {number[][]} paths
 * @return {number[]}
 */
var gardenNoAdj = function (n, paths) {
    let map = {};
    for (var i = 0; i < paths.length; i++) {
        let edge = paths[i]
        if (map[edge[0]] != undefined) {
            map[edge[0]].push(edge[1]);
        } else {
            map[edge[0]] = [edge[1]];
        }

        if (map[edge[1]] != undefined) {
            map[edge[1]].push(edge[0]);
        } else {
            map[edge[1]] = [edge[0]];
        }
    }

    let flowerKinds = [1, 2, 3, 4];
    let res = [];
    for (var i = 1; i <= n; i++) {
        let neighbors = map[i];
        let candidate = flowerKinds;
        if (neighbors != undefined) {
            let neighborFlowers = neighbors.map(n => res[n -1]).filter(f => f != undefined)
            candidate = candidate.filter(f => neighborFlowers.indexOf(f) < 0)
        }
        res[i - 1] = candidate[0];
    }

    return res;

};

let testcases = [
    {
        input: {
            n: 3,
            paths: [[1, 2], [2, 3], [3, 1]]
        }
    },
    {
        input: {
            n: 4,
            paths: [[1, 2], [3, 4]]
        }
    },
    {
        input: {
            n: 4,
            paths: [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
        }
    }
]

testcases.forEach(p => {
    let res = gardenNoAdj(p.input.n, p.input.paths)
    console.log(res)
})